import random
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render
from django.db import connection
import requests
import json
import time
import os
from django.http import HttpResponseRedirect
from django.urls import reverse
from yxb import common_tools

aaa = """
docker-compose up -d   #  -d表示后台启动
docker-compose stop     # 停止
docker-compose start   # 启动
docker-compose restart # 重启
docker-compose down     # 停止容器并移除

如果我们不指定容器名称或 ID下面的命令将停止所有容器
docker compose stop
我们可以通过指定容器名称或 ID 来指定停止哪个容器：
docker compose stop xxx

docker compose start 和 docker compose up 的区别
docker compose start  命令仅适用于已创建的容器
docker compose up   启动一个未创建的容器,  当我们使用 docker compose up 时，如果源 YAML 文件有任何变化，基于该 YAML 文件的容器将被停止并重新创建。

进入容器 (mymysql 是你自己命令的容器名称)
docker exec -it mymysql  /bin/bash

退出 docker-compose 容器:
exit 或者按下 Ctrl + D
"""

class Obj:
    def __init__(self, contenttt , pre_id , next_id):
        self.contenttt = contenttt
        self.pre_id = pre_id
        self.next_id = next_id
 


class File_object:
    def __init__(self, file_name, title, content, score):
        self.file_name = file_name
        self.title = title
        self.content = content
        self.score = score

def get_file_level_400_500():
    myList = []
    current_path = os.path.abspath(__file__)
    root_folder_path = os.path.dirname(os.path.dirname(current_path))
    root_folder_path = root_folder_path + "\TXT\\"
    file_names = [file for file in os.listdir(root_folder_path) if os.path.isfile(os.path.join(root_folder_path, file))]
    for file_name in file_names:
        # print(file_name)
        file_contentttt = get_file_content(file_name)
        if ("500" in file_contentttt ) or ("400" in file_contentttt):
            myList.append(file_name)
    random.shuffle(myList)
    random.shuffle(myList)
    return myList[0]

def get_file_content(file_id___):
    current_path = os.path.abspath(__file__)
    root_folder_path = os.path.dirname(os.path.dirname(current_path))
    root_folder_path = root_folder_path + "\TXT\\" + file_id___
    print(root_folder_path)  # C:\Users\yxb\Desktop\english_django
    all_content = ""
    with open(root_folder_path, "r", encoding="utf-8") as f:
        all_content = f.read()
    # print(all_content)
    return all_content

def get_now_pre_next_id(file_id__ ):
    current_path = os.path.abspath(__file__)
    root_folder_path = os.path.dirname(os.path.dirname(current_path))
    root_folder_path = root_folder_path + "\TXT\\"
    file_names = [file for file in os.listdir(root_folder_path) if os.path.isfile(os.path.join(root_folder_path, file))]
    mylisttt = []
    for file_name in file_names:
        mylisttt.append(file_name)

    indexxx = mylisttt.index(file_id__)

    id_list = []
    now_item_file_id = mylisttt[indexxx]
    pre_item_file_id = mylisttt[indexxx - 1]
    # # # ## # # ## # # ## # # #   
    try:
        next_item_file_id = mylisttt[indexxx + 1]
    except:
        print("BBBBBBBBBBBBBBBBBBBB")  
        next_item_file_id = mylisttt[indexxx]

    id_list.append(now_item_file_id)
    id_list.append(pre_item_file_id)
    id_list.append(next_item_file_id)
    return id_list


def index(request):
    file_id = request.GET.get('file_id', None)
    print(file_id)
    id_listtt = get_now_pre_next_id(file_id)
    now_item_file_id = id_listtt[0]
    pre_item_file_id = id_listtt[1]
    next_item_file_id = id_listtt[2]
    alll_contentt = get_file_content(now_item_file_id)
    return_data = {
        'title':' 大文本排版换行测试 ( and SQL 测试) : ',
        'file_id': file_id ,
        "aaa" : json.dumps(aaa),
        "all_content" : json.dumps(alll_contentt),
        "pre_item_file_id" :  pre_item_file_id,
        "next_item_file_id" : next_item_file_id ,
        "get_file_level_400_500" : get_file_level_400_500(),
    }
    return render(request, 'edit/index.html', context=return_data)



@csrf_exempt
def update(request):
    if request.method == 'POST':
        print("update.....")
        post_data = json.loads(request.body.decode('utf-8'))
        string_data = post_data.get('string_data', '')
        string_data = common_tools.clean_string(string_data)

        select_value = post_data.get('select_value', '')
        file_id = post_data.get('file_id', '')
        print(string_data)
        print(select_value)
        print(file_id)
        current_path = os.path.abspath(__file__)
        parent_folder = os.path.dirname(os.path.dirname(current_path))
        print("父文件夹:  ")
        print(parent_folder + "\TXT")
        file_full_path = parent_folder + "\TXT" + "\\" + file_id
        print(file_full_path)
        common_tools.write_to_txt_file(string_data, select_value, file_full_path)
        return HttpResponse("success", content_type="application/json")
    else:
        return HttpResponse("false", content_type="application/json")


"""
    api_request = requests.get("http://www.baidu.com")
    resp = {
        'title': '    title 标题  >>>  ',
        "key": api_request.content
    }
    obj_0 = File_object("file_00", "Docker 安装 Java JDK 8","Docker 的旧版本被称为 docker，docker.io 或 docker-engine 。如果已安装，请卸载它们：", 56)
    obj_1 = File_object("file_11", "JavaScript 教程","JavaScript 是 Web 的编程语言。所有现代的 HTML 页面都可以使用 JavaScript。", 12)
    print(obj_0.title)
"""
# @csrf_exempt
# def ajax_new(request):
#     if request.method == 'POST':
#         post_data = json.loads(request.body.decode('utf-8'))
#         string_data = post_data.get('string_data', '')
#         select_value = post_data.get('select_value', '')
#         print(string_data)
#         print(select_value)
#         current_path = os.path.abspath(__file__)
#         parent_folder = os.path.dirname(os.path.dirname(current_path))
#         print("父文件夹:  ")
#         print(parent_folder + "\TXT")
#         time_stamp = str(round(time.time() * 1000))   # 1700477634782 时间戳
#         file_full_path = parent_folder + "\TXT" + "\\" + str(time_stamp) + ".txt"
#         print(file_full_path)
#         common_tools.write_to_txt_file(string_data, select_value, file_full_path)
#         return HttpResponse("success", content_type="application/json")
#     else:
#         return HttpResponse("false", content_type="application/json")


# @csrf_exempt
# def ajax_post_test(request):
#     if request.method == 'POST':
#         print("ajax test =============================")
#         return HttpResponse("哈哈哈哈哈哈哈哈哈", content_type="application/json")
#     else:
#         return HttpResponse("哈哈哈哈哈哈哈哈哈", content_type="application/json")


# def get_aaa(request, aaa):
#     # 使用aaa参数的值进行处理
#     print(aaa)
#     print(aaa)
#     return HttpResponse(aaa, content_type="application/json")
